package cn.mdmm.controller.news;

import java.util.Date;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.mdmm.common.MessageConstant;
import cn.mdmm.common.SpringSecurityUtils;
import cn.mdmm.controller.BaseController;
import cn.mdmm.entity.news.NewsInfoModel;
import cn.mdmm.entity.news.request.NewsInfoRequest;
import cn.mdmm.entity.response.ResponseBodyToGridData;
import cn.mdmm.entity.response.ResponseBodyToOne;
import cn.mdmm.entity.system.SysUser;
import cn.mdmm.service.news.NewsInfoService;
import cn.mdmm.service.system.SysDictService;
import cn.mdmm.service.type.TypeInfoService;
import cn.modoumama.common.annotation.OpLog;
import cn.modoumama.common.exception.ServiceException;
import cn.modoumama.common.exception.SystemException;
import cn.modoumama.common.utils.JSONUtils;
import cn.modoumama.page.GridDataModel;
import cn.modoumama.page.Page;

@Controller
@RequestMapping(value = "/news")
@OpLog(logDesc = "新闻管理")
public class NewsInfoController extends BaseController {

	@Autowired
	NewsInfoService newsInfoService;
	@Autowired
	TypeInfoService typeInfoService;
	@Autowired
	private SysDictService sysDictService;

	private Map<String, String> typeMap;

	public void init() {
		typeMap = typeInfoService.getAllTypeToMap(1);
	}

	/**
	 * 新闻页面
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "init")
	public String init(HttpServletRequest request) {
		init();
		Map<String, String> statusMap = sysDictService.getDetailValueMap("发布状态");
		Map<String, String> levelMap = sysDictService.getDetailValueMap("级别");
		request.setAttribute("statusMap", JSONUtils.toJson(statusMap));
		request.setAttribute("statusCombo", JSONUtils.toCombo(statusMap));
		request.setAttribute("typeMap", JSONUtils.toJson(typeMap));
		request.setAttribute("typeCombo", JSONUtils.toCombo(typeMap));
		request.setAttribute("levelMap", JSONUtils.toJson(levelMap));
		request.setAttribute("levelCombo", JSONUtils.toCombo(levelMap));
		return "news/init";
	}

	/**
	 * 获取新闻列表
	 * 
	 * @param request
	 * @param fundName
	 * @param type
	 * @param level
	 */
	@RequestMapping(value = "list")
	@ResponseBody
	@OpLog(logDesc = "获取新闻列表")
	public ResponseBodyToGridData findByConditions(HttpServletRequest request, String title, Integer type,
			Integer level) {
		ResponseBodyToGridData responseBodyToGridData = new ResponseBodyToGridData();
		try {
			Page po = getPage(request, null);
			po.addCondition("title", title);
			po.addCondition("type", type);
			po.addCondition("level", level);
			GridDataModel gridDataModel = newsInfoService.findGridDataModelByCondition(po);
			responseBodyToGridData.setData(gridDataModel);
		} catch (Exception e) {
			logger.error("获取新闻列表", e);
			responseBodyToGridData.addErrInfo("999", "获取新闻列表异常", null);
		}
		return responseBodyToGridData;
	}

	@RequestMapping(value = "showAdd")
	public String showAdd(HttpServletRequest request) {
		request.setAttribute("typeCombo", JSONUtils.toCombo(typeMap));
		request.setAttribute("type", 1);
		return "news/add";
	}

	/**
	 * 新增新闻
	 * 
	 * @param request
	 * @param newsInfo
	 * @return
	 */
	@RequestMapping(value = "insert")
	@OpLog(logDesc = "新增新闻")
	public String insert(HttpServletRequest request, NewsInfoModel newsInfo) {
		Date curr = new Date();
		// 当前登录的用户
		SysUser sysUser = SpringSecurityUtils.getCurrentUser();
		newsInfo.setLevel(0);
		newsInfo.setCreateBy(sysUser.getId());
		newsInfo.setCreateDate(curr);
		newsInfo.setEditor(sysUser.getId());
		newsInfo.setEditDate(curr);
		newsInfo.setBrowseNum(0);
		newsInfo.setColletNum(0);
		if (newsInfo.getStatus() != null && newsInfo.getStatus() == 2) {
			newsInfo.setPublishDate(curr);
		}
		try {
			newsInfoService.insert(newsInfo);
		} catch (SystemException e) {
			return "error";
		}
		return "success";
	}

	@RequestMapping(value = "showEdit")
	public String showEdit(HttpServletRequest request, Long id) {
		request.setAttribute("typeCombo", JSONUtils.toCombo(typeMap));
		NewsInfoModel news = newsInfoService.findById(id);
		request.setAttribute("news", news);
		return "news/edit";
	}

	/**
	 * 修改新闻
	 * 
	 * @param request
	 * @param newsInfo
	 * @return
	 */
	@RequestMapping(value = "update")
	@OpLog(logDesc = "修改新闻")
	public String update(HttpServletRequest request, NewsInfoModel newsInfo) {
		Date curr = new Date();
		// 当前登录的用户
		SysUser sysUser = SpringSecurityUtils.getCurrentUser();
		newsInfo.setEditor(sysUser.getId());
		newsInfo.setEditDate(curr);
		if (newsInfo.getStatus() != null && newsInfo.getStatus() == 2) {
			newsInfo.setPublishDate(curr);
		}
		try {
			newsInfoService.mergeById(newsInfo);
		} catch (SystemException e) {
			return "error";
		}
		return "success";
	}

	/**
	 * 修改状态或级别
	 * 
	 * @param request
	 * @param ids
	 * @param newsInfo
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("updateReceive")
	@ResponseBody
	@OpLog(logDesc = "批量修改新闻")
	public ResponseBodyToOne updateReceive(HttpServletRequest request, Long[] ids, NewsInfoModel newsInfo)
			throws Exception {
		ResponseBodyToOne responseBody = new ResponseBodyToOne();
		logger.debug("method: updateReceive()");
		try {
			// 当前登录的用户
			SysUser sysUser = SpringSecurityUtils.getCurrentUser();
			if (newsInfo.getStatus() != null || newsInfo.getLevel() != null) {
				newsInfoService.updateNewsInfo(ids, newsInfo, sysUser.getId());
			}

		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new SystemException("修改新闻失败");
		}
		return responseBody;
	}

	@RequestMapping(value = "delete")
	@ResponseBody
	@OpLog(logDesc = "删除新闻")
	public ResponseBodyToOne delete(HttpServletRequest request, Long[] ids) {
		ResponseBodyToOne responseBody = new ResponseBodyToOne();
		logger.debug("method: delete() ");
		try {
			newsInfoService.removeByIds(ids);
		} catch (Exception e) {
			throw new ServiceException(MessageConstant.SYSTEM_ERROR);
		}
		return responseBody;
	}

	@RequestMapping(value = "info")
	@OpLog(logDesc = "新闻详细页面")
	public String info(HttpServletRequest request, NewsInfoRequest news) {
		ResponseBodyToOne re = (ResponseBodyToOne) newsInfoService.getNewsInfo(news);
		if (re != null) {
			request.setAttribute("news", re.getData());
		}

		return "news/info";
	}
}
